package com.bukaedu.info.manager.service;

import com.bukaedu.info.manager.dao.StudentDao;
import com.bukaedu.info.manager.domain.Student;

public class StudentService {

    private StudentDao studentDao = new StudentDao();

    public boolean isExists(String id) {
        Student[] stus = studentDao.findAllStudent();
        // exists  true 代表id存在  false 代表不存在
        boolean exists = false;
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
//            System.out.println("stu:" + stu);
            if(stu != null && stu.getId().equals(id)){
                exists = true;
                break;
            }
        }
        return exists;
    }

    public boolean addStudent(Student stu) {
//        System.out.println("serv-add-接收到的stu："+ stu);
        return studentDao.addStudent(stu);
    }

    public Student[] findAllStudent() {
        Student[] stus = studentDao.findAllStudent();
        // 在service这一层也注重一下数据的有效性，有值才传递
        // 遇到真实的值就改为true , 最后依然为false 证明， 数组为空
        boolean flag = false;
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            if(stu != null){
                flag = true;
                break;
            }
        }
        if(flag){
            // 有数据
            return stus;
        }else{
            //没数据
            return null;
        }
    }

    public void deleteStudentById(String delId) {
         studentDao.deleteStudentById(delId);
    }

    public void updateStudent(String updateId, Student newStu) {
        studentDao.updateStudent(updateId,newStu);
    }
}
